Combobox就是組合框,是tkinter.ttk的空件,所以要先import才可以用。他跟openmenu很像,也是一種下拉式選單。
♠♣今天的文章大綱♥♦
語法:Combobox( master, option, ... )
參數 | 說明 |
---|---|
textvariable | 以變數方式顯示選項鈕文字。 |
value | 選項鈕的值,可以區分所選取的選項鈕。 |
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')
cb = ttk.Combobox (root)
cb.pack (pady = 10)
cb_value = ("Badminton", "baseball", "basketball"," football",
" handball"," hockey"," table tennis"," tennis"," volleyball")
cb['value'] = cb_value
cb.current(0)
root.mainloop()
執行結果⬇⬇⬇
用get獲得內容
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('cuteluluWindow')
root.configure(bg="#7AFEC6")
root.iconbitmap('heart_green.ico')
root.geometry('300x100')
def get():
lb['text'] = str(cb.get())
cb = ttk.Combobox(root, state='readonly')
cb.pack()
cb['value'] = ("Badminton", "baseball", "basketball"," football",
" handball"," hockey"," table tennis"," tennis"," volleyball")
lb = tk.Label(root, text="show here",fg="#FFAAD5", bg="#7AFEC6",font=("Ravie",10,"bold"))
lb.pack()
bt = tk.Button(root, text='Get', command=get)
bt.pack()
root.mainloop()
執行結果⬇⬇⬇
選項有改變時會產生虛擬事件,所以就用這個事件去做綁定。
cb.bind("<<ComboboxSelected>>", get)
今天的combobox跟昨天openmenu真的很像,不過combobox有一個綁定的方法,可以多練習看看喔~